import hashlib
from util.singelMode import singleton
 
@singleton
class CConsensus:
    def __init__(self):
        pass

    def __call__(self):
        return self

    def proof_of_work(self,last_proof):
            proof = 0
            while self.valid_proof(last_proof, proof) is False:
                proof += 1

            return proof
        
    @staticmethod
    def valid_proof(last_proof,proof):
        guess = f'{last_proof}{proof}'.encode()
        guess_hash = hashlib.sha256(guess).hexdigest()
        return guess_hash[:4] == "0000"